package com.dharma.BIO;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Date;
import java.util.Optional;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class BIOClient {

    public static void bioClient() {

        ThreadPoolExecutor threadPoolExecutor=new ThreadPoolExecutor(5,10,2,
                TimeUnit.SECONDS,new LinkedBlockingQueue<>(5));

        // TODO 创建多个线程，模拟多个客户端连接服务端
        threadPoolExecutor.execute(() -> {
            try {
                Socket socket = new Socket("127.0.0.1", 3333);
                while (true) {
                    try {
                        socket.getOutputStream().write((new Date() + ": hello world").getBytes());
                        Thread.sleep(2000);
                    } catch (Exception e) {
                        System.out.println("Error");
                    }
                }
            } catch (IOException e) {
                System.out.println("Error");
            }
        });

    }

}
